<?php

$this->widget('zii.widgets.grid.CGridView', array(
        'id'=>'product-list',
        'dataProvider'=>$model->search(10),
        'filter' => $model,
        'afterAjaxUpdate'=>'afterGridUpdate',
        'columns'=>array(
            array(
                'header'=>'',
                'value'=>'$this->grid->dataProvider->pagination->currentPage * $this->grid->dataProvider->pagination->pageSize + ($row+1)'
            ),
            array(
                'id' => 'autoId',
                'class' => 'CCheckBoxColumn',
                'selectableRows' => '50',
            ),
            array(
                'name' => 'categoryId',
                'value' => 'isset($data->category)?$data->category->name:""',
                'filter' => CHtml::activeDropDownList($model, 'categoryId', $listData),
            ),
            array(
                'name'=>'code',                
            ),
            array(
                'name'=>'name'
            )
        ),
        'htmlOptions'=>array(
            'style'=>'width:600px; display:inline-block'
        )
));
?>

<?php $form = $this->beginWidget('CActiveForm', array(
        
        ));
?>
<?php echo CHtml::hiddenField('productIds', '', array('id'=>'product-ids')) ?>
<div class="button" style="">
    <?php echo CHtml::ajaxSubmitButton(Yii::t('main', 'Add product'), array('/admin/product/addProductPlus', 'masterId' => $masterModel->id, 'plusType'=>$plusType, 'listId'=>$listId), array('beforeSend'=>'beforeAdd', 'success' => 'reloadGrid', 'dataType'=>'json'), array('id'=>'btnsubmit')); ?>    
    
</div>

<?php $this->endWidget() ?>

<script type="text/javascript">
    var chosedArray=[];
    $(function(){        
        $('#btnsubmit').attr('disabled', 'disabled');
                
        $.ajax({
            url:'<?php echo Yii::app()->createUrl('admin/product/getProductPlusIds', array('masterId'=>$masterModel->id, 'plusType'=>$plusType)) ?>',
            dataType:'json',
            success:function(data){
                if(data.success===1){
                    $('#product-ids').val(data.data);
                    chosedArray=data.data.split(",");
                    $('#btnsubmit').removeAttr('disabled');
                    
                    $('input[type="checkbox"]').click(function(){
                        if($(this).is(':checked')){
                            addProduct($(this).val());
                        }else{
                            removeProduct($(this).val());
                        }
                    });
                    
                    initCheck();
                }else{
                    console.log(data);
                    alert('Error while get data');                    
                }
            }
        });     
    });
    
    function addProduct(id){
        chosedArray.push(id);
        $('#product-ids').val(chosedArray.join(","));
    }
    
    function removeProduct(id){
        for(var i=0;i<chosedArray.length;i++){
            if($.trim(chosedArray[i])===$.trim(id)){
                chosedArray.splice(i,1);
                break;
            }
        }
        
        $('#product-ids').val(chosedArray.join(","));
    }
    
    function beforeAdd(){
        $('#btnsubmit').attr('disabled', 'disabled');
    }
    
    function reloadGrid(data){
        console.log(data);
        if(data.success===1){
            if(window.opener!==null && typeof window.opener!=='undefined'){
                window.opener.reloadGrid('<?php echo $listId ?>');                
            }
            
            window.close();
        }else{
            alert('Have errors');
        }
    }
    
    function initCheck(){    
        $('input[type="checkbox"]').each(function(){
            for(var i=0;i<chosedArray.length;i++){
                if($(this).val()===$.trim(chosedArray[i])){
                    $(this).prop('checked', true);
                }
            }
        });
    }
    
    function afterGridUpdate(id, data){
        initCheck();
        $('input[type="checkbox"]').click(function(){
            if($(this).is(':checked')){
                addProduct($(this).val());
            }else{
                removeProduct($(this).val());
            }
        });
    }
</script>